/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Provides actions for an Alias Item
 * 
 * --CHANGELOG--
 * 
 * */
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using DOMercury.Interfaces;
using DOMercury.Forms;
using DOMercury.Managers;
using DOMercury.Items;

namespace DOMercury.Actions
{
    public class AliasExecuteAction: Interfaces.IAction
    {
        #region IAction Members
        public bool Perform(IItem a, object specialParams)
        {
            try
            {
                if (((Alias)(a)).Parameters.Contains("$"))
                {
                    MultiParams(((Alias)(a)).Parameters);
                    return true;
                }
                string drive = Path.GetPathRoot(Application.ExecutablePath);
                System.Diagnostics.Process.Start(((Alias)a).Path.Replace("^D^", drive), ((Alias)a).Parameters.Replace("^I^", (string)specialParams).Replace("^U^", System.Web.HttpUtility.UrlEncode((string)specialParams)));
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void MultiParams(string line)
        {
            string[] aliases = line.Split('$');
            foreach (string s in aliases)
            {
                try
                {
                    Alias t = AliasManager.GetByName(s.ToLower().Trim());
                    string drive = Path.GetPathRoot(Application.ExecutablePath);
                    System.Diagnostics.Process.Start(t.Path.Replace("^D^", drive), t.Parameters);
                }
                catch { }
            }
        }

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Execute";
            }
        }

        public string Description
        {
            get
            {
                return "Executes the Alias";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Command Line Parameters";
            }
        }
        #endregion
    }

    public class AliasAddAction : Interfaces.IAction
    {
        #region IAction Members
        public bool Perform(IItem a, object specialParams)
        {
            bool result;
            //Show Add Alias Dialog
            using (AliasForm sf = new AliasForm(((TextItem)a).Text))
            {
                sf.ShowDialog();
                if (sf.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        result = AliasManager.Add(sf.AliasName, sf.Path, sf.Parameters, sf.Description);

                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("Attempt to add new alias failed: " + ex.Message);
                        result = false;
                    }
                }
                else
                {
                    result = true;
                }
            }
            if (result == false)
            {
                System.Windows.Forms.MessageBox.Show("Alias Already in use.");
            }
            return result;
            //return false is aliasname is already used
        }

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get 
            { 
                return "Create Alias"; 
            }
        }
        public string Description
        {
            get
            {
                return "Adds an Alias";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Command Line Parameters";
            }
        }
        #endregion
    }

    public class AliasEditAction : Interfaces.IAction
    {
        #region IAction Members
        public bool Perform(IItem a, object specialParams)
        {
            using (AliasForm sf = new AliasForm(((Alias)a)))
            {
                int index = AliasManager.AliasList.IndexOf((Alias)a);
                sf.ShowDialog();
                if (sf.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    AliasManager.AliasList[index].AliasName = sf.AliasName;
                    AliasManager.AliasList[index].Path = sf.Path;
                    AliasManager.AliasList[index].Parameters = sf.Parameters;
                    AliasManager.AliasList[index].Description = sf.Description;
                    AliasManager.Save();
                }
            }
            return true;
           
        }

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Edit";
            }
        }

        public string Description
        {
            get
            {
                return "Edits the Alias";
            }
        }
        public string ParamDescription
        {
            get
            {
                return "Command Line Parameters";
            }
        }
        #endregion
    }

    public class AliasDeleteAction : Interfaces.IAction
    {
        #region IAction Members
        public bool Perform(IItem a, object specialParams)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to delete Alias " + ((Alias)(a)).AliasName + "?", "DOMercury Alias Delete Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    AliasManager.Delete((Alias)a);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Delete";
            }
        }

        public string Description
        {
            get
            {
                return "Deletes the Alias";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Command Line Parameters";
            }
        }

        #endregion
    }
}
